package com.example.springbott.service;

import com.example.springbott.bean.*;
import com.example.springbott.bean.common.BasePageInfo;
import com.example.springbott.bean.common.CommonData;
import com.example.springbott.bean.silic.*;
import com.example.springbott.bean.zeng.AdminOrderReplyBO;
import com.example.springbott.mapper.MarketOrderGoodsMapper;
import com.example.springbott.mapper.MarketOrderMapper;
import com.example.springbott.mapper.MarketUserMapper;
import com.example.springbott.util.Count;
import com.example.springbott.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author silic xia
 * @since 2022/11/21 22:03
 */
@Service
public class AdminOrderServiceImpl implements AdminOrderService {

    @Autowired
    MarketOrderMapper orderMapper;
    @Autowired
    MarketOrderGoodsMapper orderGoodsMapper;
    @Autowired
    MarketUserMapper userMapper;

    /**
     * List
     *
     * @param /page=1&limit=20 &timeArray=   &sort=add_time&order=desc   &orderId=没用
     *                         &orderStatusArray=订单状态 &start=始日期&end=末日期&userId=1&orderSn=订单编号
     * @return BaseRespVo -- CommonData -- MarketOrder
     * @author silic xia
     * @since 2022/11/21 21:38
     */
    @Override
    public CommonData<MarketOrder> adminOrderList(BasePageInfo info, AdminOrderListBo adminOrderListBo) {
        PageHelper.startPage(info.getPage(), info.getLimit());

        MarketOrderExample example = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = example.createCriteria();
        example.setOrderByClause(info.getSort() + " " + info.getOrder());

        if (adminOrderListBo.getOrderStatusArray() != null || "".equals(adminOrderListBo.getOrderStatusArray()))
            criteria.andOrderStatusEqualTo(adminOrderListBo.getOrderStatusArray());
        if (adminOrderListBo.getUserId() != null || "".equals(adminOrderListBo.getUserId())) {
            criteria.andUserIdEqualTo(adminOrderListBo.getUserId());
        }
        if (!StringUtil.isEmpty(adminOrderListBo.getOrderSn())) {
            criteria.andOrderSnEqualTo(adminOrderListBo.getOrderSn());
        }
        if (adminOrderListBo.getStart() != null || adminOrderListBo.getEnd() != null) {
            criteria.andPayTimeBetween(adminOrderListBo.getStart(), adminOrderListBo.getEnd());
        }
        List<MarketOrder> marketOrders = orderMapper.selectByExample(example);

        PageInfo pageInfo = new PageInfo(marketOrders);
        return CommonData.data(pageInfo);
    }


    /**
     * 按details查询订单
     *
     * @param /id=1 用like
     * @return
     * @author silic xia
     * @since 2022/11/21 22:40
     */
    @Override
    public OrderGoodsWithUserVo adminOrderDetail(String id) {
        OrderGoodsWithUserVo orderGoodsWithUserVo = new OrderGoodsWithUserVo();
        MarketOrder order = orderMapper.selectByPrimaryKey(Integer.valueOf(id));


        MarketOrderGoodsExample example = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(order.getId());
        List<MarketOrderGoods> marketOrderGoods = orderGoodsMapper.selectByExample(example);


        MarketUser marketUser = userMapper.selectByPrimaryKey(order.getUserId());
        AdminInfoBean user = new AdminInfoBean(marketUser.getNickname(), marketUser.getAvatar());

        orderGoodsWithUserVo.setOrder(order);
        orderGoodsWithUserVo.setOrderGoods(marketOrderGoods);
        orderGoodsWithUserVo.setUser(user);
        return orderGoodsWithUserVo;
    }


    /**
     * 发货
     *
     * @param {"orderId":15,"shipChannel":"YTO","shipSn":"888888888888"} 更改ship_time  order_status
     * @return {"errno":0,"errmsg":"成功"}
     * @author silic xia
     * @since 2022/11/21 23:23
     */
    @Override
    public void adminOrderShip(OrderShipBo orderShipBo) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setShipTime(new Date());
        marketOrder.setShipChannel(orderShipBo.getShipChannel());
        marketOrder.setShipSn(orderShipBo.getShipSn());
        marketOrder.setOrderStatus(Count.ORDER_STATUS_SHIPPED);

        MarketOrderExample example = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(orderShipBo.getOrderId());

        orderMapper.updateByExampleSelective(marketOrder, example);
    }

    /**
     * delete
     *
     * @param {"orderId":19 orderStatusArray: 101   未付款
     *                      orderStatusArray: 102   用户取消
     *                      orderStatusArray: 103   系统取消
     *                      orderStatusArray: 201   已付款
     *                      orderStatusArray: 202   申请退款
     *                      orderStatusArray: 203   已退款
     *                      orderStatusArray: 301  已发货
     *                      orderStatusArray: 401 用户收货
     *                      orderStatusArray: 402 系统收货
     * @return {"errno":623,"errmsg":"订单不能删除"}
     * {"errno":0,"errmsg":"成功"}orderStatusArray: 101
     * @author silic xia
     * @since 2022/11/22 0:04
     */
    @Override
    public int adminOrderDelete(Integer orderId) {
        MarketOrder order = orderMapper.selectByPrimaryKey(orderId);
        MarketOrderExample example = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = example.createCriteria();
        for (Short aShort : Count.ORDER_STATUS_LIST) {
            if (aShort.equals(order.getOrderStatus())) {
                MarketOrder marketOrder = new MarketOrder();
                marketOrder.setDeleted(true);
                int code = orderMapper.updateByExampleSelective(marketOrder, example);
                return code;
            }
        }
        return Count.ORDER_DELETE_ERROR;

    }

    /**
     * 退款refund
     *
     * @param {"orderId":15,"refundMoney":101}
     * @return
     * @author silic xia
     * @since 2022/11/22 10:38
     */
    @Override
    public int adminOrderRefund(OrderRefundBo orderRefundBo) {
        MarketOrder order = orderMapper.selectByPrimaryKey(orderRefundBo.getOrderId());
        if (Count.ORDER_STATUS_REFUNDABLE != order.getOrderStatus()) {
            return Count.ORDER_REFUND_ERROR;
        }
        MarketOrderExample example = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(orderRefundBo.getOrderId());
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setRefundAmount(orderRefundBo.getRefundMoney());
        marketOrder.setRefundTime(new Date());
        marketOrder.setOrderStatus(Count.ORDER_STATUS_REFUNDED);
        int code = orderMapper.updateByExampleSelective(marketOrder, example);
        return code;
    }

    @Override
    public boolean AdminOrderReplyComment(AdminOrderReplyBO adminOrderReplyBO) {
        String adminComment =orderMapper.adminOrderReplyCommentIsNull(adminOrderReplyBO.getCommentId());
        if (adminComment==null||adminComment.equals("")){
            orderMapper.addAdminOrderReplyComment(adminOrderReplyBO);
            return true;
        }else {
            return false;
        }
    }
}

